<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="500px"
      @confirm="handleSubmit"
      @close="handleClose"
      style="max-height: 60vh"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="90px"
        :rules="formRules"
        :disabled="popupTitle == '员工详情'"
      >
        <el-form-item label="头像" prop="avatar">
          <material-picker
            :disabled="popupTitle == '员工详情'"
            v-model="formData.avatar"
            :limit="1"
          />
        </el-form-item>
        <el-form-item label="用户名" prop="account">
          <el-input
            v-model="formData.account"
            clearable
            placeholder="请填写账号"
          />
        </el-form-item>
        <el-form-item
          label="密码"
          v-show="popupTitle !== '员工详情'"
          :prop="popupTitle === '新增员工' ? 'password' : ''"
        >
          <el-input
            v-model="formData.password"
            clearable
            placeholder="请填写密码"
          />
        </el-form-item>
        <!-- <el-form-item label="角色" :prop="popupTitle === '新增用户表' ? 'role_id' : ''">
          <el-select class="flex-1" v-model="formData.role_id" clearable placeholder="请选择角色">
            <el-option
              v-for="(item, index) in roles"
              :key="index"
              :label="item.role_name"
              :value="parseInt(item.id)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="昵称" :prop="popupTitle === '新增用户表' ? 'nickname' : ''">
          <el-input v-model="formData.nickname" clearable placeholder="请填写昵称" />
        </el-form-item>-->
        <el-form-item label="电话" prop="mobile">
          <el-input
            v-model="formData.mobile"
            clearable
            placeholder="请填写电话"
          />
        </el-form-item>
        <!-- <el-form-item label="邮箱" prp="email">
          <el-input v-model="formData.email" clearable placeholder="请填写邮箱" />
        </el-form-item>
        <el-form-item label="渠道来源" prop="comefrom">
          <el-input v-model="formData.comefrom" clearable placeholder="请填写渠道来源" />
        </el-form-item>
        <el-form-item label="等级" prop="level_i">
          <el-select class="flex-1" v-model="formData.level_id" clearable placeholder="请选择级别">
            <el-option
              v-for="(item, index) in dictData.level"
              :key="index"
              :label="item.name"
              :value="parseInt(item.value)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="类型" :prop="popupTitle === '新增用户表' ? 'type' : ''">
          <el-select v-model="formData.type" clearable placeholder="请选择类型">
            <el-option
              v-for="(item, index) in dictData.type"
              :key="index"
              :label="item.name"
              :value="parseInt(item.value)"
            />
          </el-select>
        </el-form-item>-->
        <!-- <el-form-item label="时间" prop="update_time">
          <el-date-picker
            v-model="formData.update_time"
            type="datetime"
            clearable
            placeholdr="请选择时间"
            format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>-->
        <el-form-item label="启用" prop="status">
          <el-switch
            v-model="formData.status"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label="超级管理员" prop="is_surp">
          <el-switch
            v-model="formData.is_surp"
            :active-value="1"
            :inactive-value="0"
            active-text="是"
            inactive-text="否"
          />
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>

<script lang="ts" setup name="memberEdit">
import type { FormInstance, UploadProps, ElMessage } from "element-plus";
import Popup from "@/components/popup/index.vue";
import { apiMemberRolesLists } from "@/api/member_roles";
import { apiMemberAdd, apiMemberEdit, apiMemberDetail } from "@/api/member";
import { timeFormat } from "@/utils/util";
import config from "@/config";
import type { PropType } from "vue";

defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({}),
  },
});
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");

// 弹窗标题
const popupTitle = computed(() => {
  if (mode.value == "edit") {
    return "编辑员工";
  } else if (mode.value == "add") {
    return "新增员工";
  } else if (mode.value == "detail") {
    return "员工详情";
  }
});
//获取角色名称
const roles = ref<any[]>([]);
const queryParams = reactive({
  role_name: "",
  identify: "",
  remark: "",
  sort: "",
});
const getRolesList = async () => {
  const res = await apiMemberRolesLists(queryParams);
  roles.value = res.lists;
};
getRolesList();

// 表单数据
const formData = reactive({
  avatar: "",
  id: "",
  account: "",
  password: "",
  role_id: "",
  nickname: "",
  mobile: "",
  email: "",
  level_id: "",
  comefrom: "",
  update_time: "",
  ip: "",
  address: "",
  ac_num: "",
  remark: "",
  status: 1,
  type: "",
  source: "",
  is_change: "",
  is_surp: "",
});

// 表单验证
const formRules = reactive<any>({
  account: [
    {
      required: true,
      message: "请填写账号",
      trigger: ["blur"],
    },
  ],
  password: [
    {
      required: true,
      message: "请填写密码",
      trigger: ["blur"],
    },
  ],
});

// 获取详情
const setFormData = async (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
  formData.password = "";
};

const getDetail = async (row: Record<string, any>) => {
  const data = await apiMemberDetail({
    id: row.id,
  });
  setFormData(data);
};
// 提交按钮
const handleSubmit = async () => {
  await formRef.value?.validate();
  const data = { ...formData };
  mode.value == "edit" ? await apiMemberEdit(data) : await apiMemberAdd(data);
  popupRef.value?.close();
  emit("success");
};

//打开弹窗
const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

// 关闭回调
const handleClose = () => {
  emit("close");
};
// getRolesList()

defineExpose({
  open,
  setFormData,
  getDetail,
});
</script>
